package com.winsdom.controller.CustomerManagementController;

import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.dto.CustomerBillingHeaderCreateDTO;
import com.winsdom.result.Result;
import com.winsdom.service.ICustomerBillingHeaderService;
import com.winsdom.vo.CustomerBillingHeaderQueryVO;
import com.winsdom.vo.CustomerBillingHeaderUpdateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.winsdom.enums.AuthenticationType.CUSTOMER_MANAGEMENT;
import java.util.List;

/**
 * 发票抬头控制器
 * 用于管理客户的发票抬头信息，包括查询和更新客户的发票抬头设置
 * 支持业主和平台后台两种来源的抬头管理
 */
@Slf4j
@RestController
@RequestMapping("/invoice-title")
@Api(tags = "客户管理接口", description = "客户发票抬头管理接口")
@Authentication(value=CUSTOMER_MANAGEMENT)
public class InvoiceTitleController {

    @Autowired
    private ICustomerBillingHeaderService customerBillingHeaderService;

    @GetMapping("/query")
    @ApiOperation(value = "查询客户发票抬头", notes = "根据客户ID查询其所有发票抬头信息")
    @OperationLog(description = "查询客户发票抬头")
    public Result<List<CustomerBillingHeaderQueryVO>> getCustomerBillingHeaders(@RequestParam Long customerId) {
        log.info("查询客户发票抬头，客户ID：{}", customerId);
        List<CustomerBillingHeaderQueryVO> headers = customerBillingHeaderService.getHeadersByCustomerId(customerId);
        return Result.success(headers);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新发票抬头", notes = "更新指定客户的发票抬头信息，平台后台增加的抬头可直接修改，业主增加的抬头需要审核")
    @OperationLog(description = "更新发票抬头")
    public Result<Void> updateCustomerBillingHeader(@RequestBody CustomerBillingHeaderUpdateVO updateVO) {
        log.info("更新发票抬头，更新信息：{}", updateVO);
        customerBillingHeaderService.updateHeader(updateVO);
        return Result.success();
    }

    @PostMapping("/create")
    @ApiOperation(value = "创建发票抬头", notes = "创建新的发票抬头信息，平台来源的抬头自动审核通过，业主来源的抬头需要审核")
    @OperationLog(description = "创建发票抬头")
    public Result<Long> createCustomerBillingHeader(@RequestBody CustomerBillingHeaderCreateDTO createDTO) {
        log.info("创建发票抬头，创建信息：{}", createDTO);
        Long headerId = customerBillingHeaderService.createHeader(createDTO);
        return Result.success(headerId);
    }


    @GetMapping("/toggle-status/{headerId}")
    @ApiOperation(value = "切换发票抬头状态", notes = "切换发票抬头的启用状态，只有平台来源的抬头可以修改状态")
    @OperationLog(description = "切换发票抬头状态")
    public Result<Void> toggleCustomerBillingHeaderStatus(@PathVariable Long headerId) {
        log.info("切换发票抬头状态，抬头ID：{}", headerId);
        customerBillingHeaderService.toggleHeaderStatus(headerId);
        return Result.success();
    }

    @GetMapping("delete/{headerId}")
    @ApiOperation(value = "删除发票抬头", notes = "删除发票抬头信息，只有平台来源的抬头可以删除")
    @OperationLog(description = "删除发票抬头")
    public Result<Void> deleteCustomerBillingHeader(@PathVariable Long headerId) {
        log.info("删除发票抬头，抬头ID：{}", headerId);
        customerBillingHeaderService.deleteHeader(headerId);
        return Result.success();
    }
}